//
//  AppDelegate.m
//  SportGo
//
//  Created by rimi on 16/9/20.
//  Copyright © 2016年 Yrzteam. All rights reserved.
//
#define SCREEN_SIZE  [UIScreen mainScreen].bounds.size
#import "AppDelegate.h"
#import "HomePageViewController.h"
#import "CalendarViewController.h"
#import "RecommendViewController.h"
#import "MyViewController.h"
#import <HealthKit/HealthKit.h>
//腾讯开放平台（对应QQ和QQ空间）SDK头文件
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import "IQKeyboardManager.h"
#import <ShareSDK/ShareSDK.h>
#import "WeiboSDK.h"//微博SDK头文件
#import <ShareSDKConnector/ShareSDKConnector.h>
//微信SDK头文件
#import "WXApi.h"
#import "NotificationHelper.h"
#import "HealthKitManage.h"
#import "HWPopTool.h"
#import "TabBarViewController.h"

#import <AMapFoundationKit/AMapFoundationKit.h>
#import <CoreData/NSManagedObjectContext.h>
#import <CoreData/NSManagedObjectModel.h>
#import <CoreData/NSPersistentStoreCoordinator.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [AMapServices sharedServices].apiKey =@"0079d5d556a3951260cb2f904b3d6d29";
    _SDKProcess = [[LJHSDKInitProcess alloc]initWithApplication:application andLaunchOption:launchOptions];
    
    [ShareSDK registerApp:@"1783a402b6aa0"
     
          activePlatforms:@[
                            @(SSDKPlatformTypeSinaWeibo),
                            @(SSDKPlatformTypeWechat),
                            @(SSDKPlatformTypeQQ)]
                 onImport:^(SSDKPlatformType platformType)
     {
         switch (platformType)
         {
             case SSDKPlatformTypeWechat:
                 [ShareSDKConnector connectWeChat:[WXApi class]];
                 break;
             case SSDKPlatformTypeQQ:
                 [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                 break;
             case SSDKPlatformTypeSinaWeibo:
                 [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                 break;
                 
             default:
                 break;
         }
     }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
     {
         
         switch (platformType)
         {
             case SSDKPlatformTypeSinaWeibo:
                 //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                 [appInfo SSDKSetupSinaWeiboByAppKey:@"683308339"
                appSecret:@"b438d0cc704dbf8390438e7dd2cbf688"
                redirectUri:@"http://www.weibo.com"
                                            authType:SSDKAuthTypeBoth];
                 break;
             case SSDKPlatformTypeWechat:
                 [appInfo SSDKSetupWeChatByAppId:@"wx14647f9c605ce7e3"
                                       appSecret:@"a495062495893e15c9cd78a4f6167c32"];
                 break;
             case SSDKPlatformTypeQQ:
                 [appInfo SSDKSetupQQByAppId:@"1105647779"
                                      appKey:@"UERWxo617jrQaJba"
                                    authType:SSDKAuthTypeBoth];
                 break;
             default:
                 break;
         }
     }];

    //关闭设置为NO, 默认值为NO.
    
    [IQKeyboardManager sharedManager].enable = YES;
    //隐藏键盘上的工具条
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    
    [AVOSCloud setApplicationId:@"4tISrKGao4FXuY7d6Ny9KT7S-gzGzoHsz" clientKey:@"2yo3cmfH1i9cUHwHn4eXTF3R"];
    self.autoSizeScaleX = SCREEN_SIZE.width/375;
    self.autoSizeScaleY = SCREEN_SIZE.height/667;
    UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen] .bounds];
    _window = window;
    
    TabBarViewController  *tabBar = [[TabBarViewController alloc] init];
    //主页
    HomePageViewController *homeVC = [[HomePageViewController alloc] init];
    UINavigationController *homeNav = [[UINavigationController alloc]initWithRootViewController:homeVC];
    homeVC.tabBarItem.title = @"运动";
    [homeVC.tabBarItem setImage:[UIImage imageNamed:@"首页"]];
    //日历
    CalendarViewController *calendarVC = [[CalendarViewController alloc] init];
    UINavigationController *calendarNav = [[UINavigationController alloc] initWithRootViewController:calendarVC];
    calendarVC.tabBarItem.title = @"记录";
    [calendarVC.tabBarItem setImage:[UIImage imageNamed:@"日历"]];
    //推荐
    RecommendViewController *recommendVC = [[RecommendViewController alloc] init];
    UINavigationController *recommendNav  = [[UINavigationController alloc] initWithRootViewController:recommendVC];
    recommendVC.tabBarItem.title = @"推荐";
    [recommendVC.tabBarItem setImage:[UIImage imageNamed:@"推荐"]];
    //我的
    MyViewController *myVC = [[MyViewController alloc] init];
    UINavigationController *myNav  = [[UINavigationController alloc] initWithRootViewController:myVC];
    myVC.tabBarItem.title = @"我的";
    [myVC.tabBarItem setImage:[UIImage imageNamed:@"我的"]];
    
    tabBar.viewControllers = @[homeNav,calendarNav,recommendNav,myNav];
    _window.rootViewController = tabBar;
    //查看系统版本号
//    CGFloat currentDeviceVersionFloat = [[[UIDevice currentDevice] systemVersion] floatValue];

 [[NotificationHelper shareInstance] registerLocalNotification:application];
    [AVOSCloud registerForRemoteNotification];
    application.applicationIconBadgeNumber = 0;
    [_window makeKeyAndVisible];
    return YES;
    

}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
   
    [AVOSCloud handleRemoteNotificationsWithDeviceToken:deviceToken];
    
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (void)dealloc{
    [HWPopTool clearHWPopTool];
    [NotificationHelper deallocNoticationHelper];
    [HealthKitManage deallocHealthKit];
}
#pragma mark - Core Data stack

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

- (NSURL *)applicationDocumentsDirectory {
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
- (NSManagedObjectModel *)managedObjectModel {
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"SportsGo" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}


- (NSManagedObjectContext *)managedObjectContext {
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }
    
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (!coordinator) {
        return nil;
    }
    _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    return _managedObjectContext;
}
#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        NSError *error = nil;
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}
#pragma mark - getter
+ (AppDelegate *)globalDelegate {
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
}
@end
